<?php

class RssController extends Zend_Controller_Action
{


    public function favoritosAction()
    {
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $this->_helper->layout->disableLayout();
        
               
        
        $pages = new Sgit_Navigation();
        $container=$pages->getNavigation();
                                                
        $navigation = $this->view->navigation($container);
        
        $limit=$this->_getParam('limit',15);
        $order=$this->_getParam('order');
       
        $data=$this->user();
        $id_user=$data['id'];
        $usuario=$data['usuario'];

        $m_favoritos= new Application_Model_Visitas();
        $favoritos=$m_favoritos->favoritos($id_user,$order,$limit);


        $server=Zend_Controller_Front::getInstance()->getRequest()->getServer('SERVER_NAME');
        $server="http://$server";

        $feed = new Zend_Feed_Writer_Feed;
        $text=$order ? 'Recientes' : 'Favoritos';
        
        $feed->setTitle('Viriathus - '.$text.' - '.$usuario);
        $feed->setLink($server);
        $feed->setFeedLink($server."/rss/favoritos/id/".md5($id_user.$usuario)."/order/recent", 'atom');
        $feed->addAuthor(array(
            'name'  => 'Viriathus',
            'uri'   => $server,
        ));
        $feed->setDateModified(time());
      

        
        foreach($favoritos as $link){
            $key=$link['controller'].':'.$link['action'];
            
            switch($key){
                case 'proyectosdc-post:index':
                    $proyectodc[] = substr(strrchr($link['uri'], "/"), 1);
                    break;
                case 'ofertas-post:index':
                     $oferta[] = substr(strrchr($link['uri'], "/"), 1);
                     break;
                case 'proyecto:ficha':
                    $proyecto[] = substr(strrchr($link['uri'], "/"), 1);
                    break;    
            }
        }
        
        if(is_array($proyectodc)){
            $m_proyectodc= new Application_Model_ProyectosDC();
            $proyectosdc=$m_proyectodc->getAssoc('id','proyecto',array('id IN(?)'=>$proyectodc));
        }
        if(is_array($oferta)){
            $m_ofertas= new Application_Model_Ofertas();
            $ofertas=$m_ofertas->getAssoc('id','cliente',array('id IN(?)'=>$oferta));
        }
        if(is_array($proyecto)){
            $m_proyectos= new Application_Model_Proyectos();
            $proyectos=$m_proyectos->getAssoc('id','nombre',array('id IN(?)'=>$proyecto));
        }


        foreach($favoritos as $link){
            $key=$link['controller'].':'.$link['action'];
            if($link['idZona'])
                $zona=Sgit_Cache::single()->model('Datacenter')->getKey($link['idZona']);

            switch($key){
                
                case 'proyectosdc-post:index':
                    $id = substr(strrchr($link['uri'], "/"), 1);
                    $name='P'.str_pad($id,4,'0',STR_PAD_LEFT).' '. $proyectosdc[$id];
                    $enl=$server.$link['uri'];
                    $title=$name;
                    
                    break;
                 case 'ofertas-post:index':
                    $id = substr(strrchr($link['uri'], "/"), 1);
                    $name='OF'.str_pad($id,4,'0',STR_PAD_LEFT).' '. $ofertas[$id];
                    $enl=$server.$link['uri'];
                    $title=$name;
                    break;
                case 'proyecto:ficha':
                    $id = substr(strrchr($link['uri'], "/"), 1);
                    $name='Ficha: '.$proyectos[$id];
                    $enl=$server.$link['uri'];
                    $title=$name;
                    break;
                case 'menu:publico':
                    $idZona =$link['idZona'];
                    $name=$zona.': Acceso Informes';
                    $enl=$server.$link['uri'];
                    $title=$name;
                    break;
                case 'menu:viriathus':
                    $idZona =$link['idZona'];
                    $name=$zona.': ';
                    $enl=$server.$link['uri'];
                    $title=$name;
                    break;
              
                default:
                    unset($content);
                    if($link['idZona']>0){
                        $content = $zona.': ';
                    }
                    
                    $url=$this->view->idToUrl($link['id'],null,null,true);
                    
                    if($url){
                        $enl=$server.'/'.$url['url'];
                        $title=$content.$url['text'];
                    } else{
                        $enl=$server.$link['uri'];
                        $title=$link['controller'].':'.$link['action'];
                    }
            
            }
          
                $entry = $feed->createEntry();
                $entry->setTitle($title);
                $entry->setLink($enl);
                $entry->addAuthor(array(
                    'name'  => 'Viriathus',
                    'uri'   =>$server,
                ));

                

                
                $created_on= $link['created_on'] ? strtotime($link['created_on']) :time(); 
                $entry->setDateCreated($created_on);
                
                $updated_on=$link['updated_on'] ? strtotime($link['updated_on']) : $created_on ;
                $entry->setDateModified($updated_on);
        
        
                $feed->addEntry($entry);

        }
        
    

        print $feed->export('atom');
        
    }

    public function user()
    {
        
        $id_user= $this->_getParam('id');
        if($id_user){
            $m_usuarios=new Application_Model_Usuario();
            $wh['MD5(CONCAT(id,usuario)) = ?']=$id_user;
            $user = $m_usuarios->getRow($wh);
            $id_user=$user['id'];
            $usuario=$user['usuario'];
        }else{
            $auth = Zend_Auth::getInstance();
            $id_user=$auth->getIdentity()->id;
            $usuario=$auth->getIdentity()->usuario;

        }
        
        return array('id'=>$id_user,'usuario'=>$usuario);
    }


    public function mailAction()
    {
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $this->_helper->layout->disableLayout();
        
        

        $server=Zend_Controller_Front::getInstance()->getRequest()->getServer('SERVER_NAME');
        $server="http://$server";

    
        $limit=$this->_getParam('limit',25);
        
        $data=$this->user();
        $id_user=$data['id'];
        $usuario=$data['usuario'];
        
        
        $m_mail=new Application_Model_Mail();
        
        
        $values['id_user']=$id_user;
        $values['limit']=$limit;
        $values['leido']=0;
        $rows=  $m_mail->getMails($values);


        $feed = new Zend_Feed_Writer_Feed;

        $feed->setTitle('Viriathus - Mails - '.$usuario);
        $feed->setLink($server."/mail");
        $feed->setFeedLink($server."/rss/mail/id/".md5($id_user.$usuario), 'atom');
        $feed->addAuthor('Viriathus');
        $feed->setDateModified(time());
      

        $ruta=APPLICATION_PATH . '/mail/';
        $filter     = new Zend_Filter_Decompress('Gz');


        foreach($rows as $v){
            
                $v['url']=$server.'/mail/mail/id/'.$v['id'].'_'.md5($v['asunto']);
                $file=$ruta.$v['id'].'_'.md5($v['asunto']).'.gz';
                $v['description']= $filter->filter($file);
  
                $entry = $feed->createEntry();
                $entry->setTitle($v['asunto']);
                $entry->setLink($v['url']);
                if($v['description']) $entry->setDescription($v['description']);

                $entry->setDateCreated(strtotime($v['created_on']));
                $entry->setDateModified($created_on);
        
                $feed->addEntry($entry);

        }
        
    

        print $feed->export('atom');
        
    }

}
